import AntModal from '@/components/pt/dialog/AntModal'
import { getBizParts, addBizParts, updateBizParts } from '@/api/biz/bizParts'

import { upload } from '@/api/system/upload'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import storage from 'store'
export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    }

  },
  components: {
    AntModal

  },
  data () {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      headers: { Authorization: 'Bearer ' + storage.get(ACCESS_TOKEN) },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加配件单',
      fileList:[],
      // 表单参数
      form: {},
      rules: {
        partsNumber: [{ required: true, message: '单据编号不能为空', trigger: 'blur' }],

        tenantId: [{ required: true, message: '租户id不能为空', trigger: 'blur' }],

        status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]

      }
    }
  },
  filters: {},
  created () {},
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    onClose () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        partsNumber: undefined,

        partsDate: undefined,

        address: undefined,

        phone: undefined,

        fileList: undefined,

        tenantId: undefined,

        orderId: undefined,

        status: '0'

      }
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.formTitle = '添加配件单'
    },
    handleAddAccess(form){
      console.log('666 :>> ', form);
      this.reset()
      this.open = true
      this.formTitle = '添加配件单'
      this.$nextTick(()=>{
        this.$set(this.form,'address',form.address)
        this.$set(this.form,'phone',form.phone)
      })
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const bizPartsId = row.id
      getBizParts(bizPartsId).then(response => {
        this.form = response.data
        this.formTitle = '修改配件单'
        let url = response.data && response.data.fileList || ''
        let fileList = []
        if (url) {
          fileList = url.split(',')
        } 
            fileList.forEach((item,index)=>{
              const d = {
                uid: index,
                name: item,
                url: item,
                status: 'done'
              }
              // 文件列表
              this.fileList.push(d)
        })
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          let saveForm = JSON.parse(JSON.stringify(this.form))
          let filePaths="";
            this.fileList.forEach(file=>{
              if(filePaths.length>0) {
                filePaths+=",";
              }
              filePaths+=file.url;
            })
            saveForm.fileList = filePaths
          if (this.form.id !== undefined) {
            updateBizParts(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          } else {
            addBizParts(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok',response)
              this.$emit('close')
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/biz/bizparts/index'
      this.$router.push(index)
    },
    
    handleChange(info,e) {
      const formData = new FormData();
      formData.append('file', info.file);
      upload(formData).then(response => {
          const d = {
            uid: info.file.uid,
            name: response.fileName,
            url: response.url,
            status: 'done'
          }
          this.fileList.push(d);
      })
    },
    
    handleRemove(info) {
      this.fileList = this.fileList.filter(function (item) {
        return item.uid != info.uid
      })
    },
  }
}
